﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class CommitForm : System.Windows.Forms.Form
    {
        private string rootPath;
        private int projID;

        public CommitForm(string path)
        {
            InitializeComponent();

            rootPath = path;
            LoadCommitFile();

            TxtReader.ReadDataFile(path);
            projID = Int32.Parse(TxtReader.GetString(TxtReader.LINE.PROJECTID));
        }

        // 커밋할 파일들의 목록을 체크박스리스트에 표시
        public void LoadCommitFile()
        {
            List<string> fileList = FolderAdmin.GetCommitFiles(rootPath);
            foreach (string file in fileList)
            {
                cbFileList.Items.Add(file);
            }
        }

        // 체크박스리스트의 아이템들을 모두 선택하는 이벤트
        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cbFileList.Items.Count; i++)
            {
                cbFileList.SetItemChecked(i, true);
            }
        }

        // 커밋!! ^_^
        private void btnCommit_Click(object sender, EventArgs e)
        {
            CCL.SocketMessage.Request.CommitProject packet = new CCL.SocketMessage.Request.CommitProject(projID, MainForm.userData.UserID, "");
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            CCL.SocketMessage.Message commitMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.CommitProject commitResponse = (CCL.SocketMessage.Response.CommitProject)commitMsg.Body;

            if (commitResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                CheckedListBox.CheckedItemCollection collection = cbFileList.CheckedItems;
                System.Collections.IEnumerator ea = collection.GetEnumerator();
                if(ea.MoveNext() == false)
                {
                    MessageBox.Show("파일을 선택하세요");
                    return;
                }
                for (int i = 0; i < cbFileList.CheckedItems.Count; i++)
                {
                    string absolutePath = (string)ea.Current;
                    string relativePath = absolutePath.Replace(rootPath, "");

                    CCL.SocketMessage.Request.FileUpload upPacket = new CCL.SocketMessage.Request.FileUpload(projID, commitResponse.Revision, relativePath, absolutePath);
                    message = CCL.SocketMessage.Message.MessageRequestBuilder(upPacket);
                    byte[] upMsg = message.GetByte();

                    CCL.SocketMessage.Message upPacketMsg = MainForm.connector.Execute(upMsg);
                    CCL.SocketMessage.Response.FileUpload fileUploadResponse = (CCL.SocketMessage.Response.FileUpload)upPacketMsg.Body;
                    if (fileUploadResponse.Result == CCL.SocketMessage.Constant.Result.Fail)
                    {
                        MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(fileUploadResponse.ErrorMessage));
                    }
                    
                    // 파일 경로 : 루트 + 파일명
                    //CCL.Utility.FileUtility.BytesToFile(fileDownloadResponse.FileBytes, txtDirectory.Text + fileDownloadResponse.FileName);

                    progressBar1.Value = ((i + 1) / cbFileList.CheckedItems.Count) * 100;

                    ea.MoveNext();
                }
                string[] data = { projID.ToString(), commitResponse.Revision.ToString(), System.DateTime.Now.ToString("yyyyMMddHHmmss") };
                    TxtReader.WriteDataFile(rootPath, data);
                MessageBox.Show("Complete Commit");
                this.Close();
            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(commitResponse.ErrorMessage));
            }
        }
    }
}
